<template>
    <div>
        <ListPage>
            <template #filter>
                <div class="flex-sb">
                    <div>
                        <el-form ref="form" :model="searchForm" inline label-width="80px">
                            <el-form-item label="">
                                <el-input v-model="searchForm.name" placeholder="商品名称" clearable style="width: 200px;"></el-input>
                            </el-form-item>
                            <el-form-item>
                                <el-button type="primary" size="small" @click="submitSearchForm">
                                    搜索
                                </el-button>
                                <el-button size="small">重置</el-button>
                            </el-form-item>
                        </el-form>
                    </div>
                    <div style="font-size: 28px">
                        <b>
                            总销量：<span style="color: #fc4316"
                                >{{
                                    totalSalesVolume ? totalSalesVolume : 0
                                }}件</span
                            >
                        </b>
                    </div>
                </div>
            </template>

            <template #table>
                <el-table :data="tableData" v-loading="loading" style="width: 100%" size="medium">
                    <el-table-column type="index" label="序号" width="100"></el-table-column>
                    <el-table-column prop="coverUrl" label="封面" width="">
                        <template v-slot="scope">
                            <img width="60px" :src="BaseApi + scope.row.coverUrl" />
                        </template>
                    </el-table-column>
                    <el-table-column prop="goodsName" label="商品名" :show-overflow-tooltip="true" width=""></el-table-column>
                    <el-table-column prop="salesVolume" label="销量" :show-overflow-tooltip="true" width=""></el-table-column>
                    <el-table-column prop="percentage" label="销售量占比" :show-overflow-tooltip="true" width="">
                        <template v-slot="scope">
                            <el-progress width="50" type="dashboard" :percentage="scope.row.percentage" :color="customColorMethod"></el-progress>
                        </template>
                    </el-table-column>
                </el-table>
            </template>

            <template #pagination>
                <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                    :current-page.sync="searchForm.currentPage" :page-sizes="[5, 10, 20, 50]"
                    :page-size="searchForm.pageSize" layout="total,sizes, prev, pager, next,jumper"
                    :total="searchForm.total">
                </el-pagination>
            </template>
        </ListPage>
    </div>
</template>
<script>
import { popup } from "@/assets/js/common";
import { queryPageByQo } from "@/api/goods_management/statistic/statisticSaleApi";

export default {
    data() {
        return {
            BaseApi: this.$store.state.BaseApi,
            loading: false,
            tableData: [],
            totalSalesVolume: "", //所有商品总销量
            searchForm: {
                pageSize: 5,
            },
            options_goods: [],
        };
    },
    methods: {
        /*分页*/
        init() {
            /*查询商品信息*/
            this.loading = true;
            queryPageByQo(this.searchForm).then((res) => {
                res = res.data;
                if (res.code == 200) {
                    this.totalSalesVolume = res.data.total;
                    this.tableData = res.data.vos.records;
                    this.searchForm.total = res.data.vos.total;
                    this.searchForm.pageSize = res.data.vos.size;
                    this.searchForm.currentPage = res.data.vos.current;
                } else {
                    popup(res.msg, "error");
                }
            })
            .finally(() => {
                this.loading = false;
            });
        },
        handleSizeChange(val) {
            this.searchForm.pageSize = val;
            this.init();
            console.log(`每页 ${val} 条`);
        },
        handleCurrentChange(val) {
            this.searchForm.currentPage = val;
            this.init();
            console.log(`当前页: ${val}`);
        },
        submitSearchForm() {
            this.init();
        },
        customColorMethod(percentage) {
            if (percentage < 30) {
                return "#f56c6c";
            } else if (percentage < 70) {
                return "#e6a23c";
            } else {
                return "#67c23a";
            }
        },
    },

    mounted() {
        this.init();
    },
};
</script>
<style scoped>

</style>